package com.android.goal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class LiveScoreActivity extends Activity {	

	ArrayList<League> listLeague;
	LeagueAdapter adapter;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.live_score);
	
		String input = this.getInput();
		input = input.substring(1,input.length()-2);			
		
		//League l = new League (input);
		//listLeague.add(l);		
		listLeague = new ArrayList<League>();
		this.parseJson(input);
		adapter = new LeagueAdapter(this, R.layout.leagues_view,listLeague);
		final ListView list = (ListView) findViewById(R.id.list_leagues);
		list.setAdapter(adapter);						
	}	
	
	public void parseJson(String json){
		try{				
			// chuoi json input la 1 mang cac tran dau
			JSONArray jsonArray = new JSONArray(json);	
			Map<String, League> mapLeague = new HashMap<String, League>();
			// moi object la 1 chuoi json ve thong tin tran dau
			for (int i = 0; i < jsonArray.length(); i++){
				// lay chuoi json ve thong tin tran dau thu i
				JSONObject matchJson = jsonArray.getJSONObject(i);
				Match match = new Match(matchJson);
				// bat thong tin ve ten giai dau trong object 																					    
				String competition = matchJson.getString("competition");
				League l = new League(competition);								
				if (!mapLeague.containsKey(competition)){
					mapLeague.put(competition, l);					
				}					
				mapLeague.get(competition).addMatch(match);
			}	
			
			for (String key:mapLeague.keySet()){
				League l = mapLeague.get(key);
				this.listLeague.add(l);				
			}
			//this.adapter.notifyDataSetChanged();			
		}catch(NumberFormatException e){
			System.out.println("Could not parse " + e);
		}catch(JSONException e){
			e.printStackTrace();			
		}
	}
		
	public String getInput(){		
		BufferedReader in = null;        
        try {
        	HttpClient client = new DefaultHttpClient();        
        	HttpGet request = new HttpGet();
        	request.setURI(new URI("http://10.0.2.2:8888/matches?method=GetListOfMatch&date=2012-02-01&IDLeague=&week=&numbMatch=&IDTeam="));
        	
        	HttpResponse response = client.execute(request);
        	// Luu response vao buffer
        	in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
        	
        	StringBuffer sb = new StringBuffer("");
        	String line = "";
        	String NL = System.getProperty("line.separator");
        	// Doc tung dong vao string buffer
        	while ((line = in.readLine()) != null) {
        		sb.append(line + NL);
        	}       
        	in.close();        	
        	String page = sb.toString();
        	//System.out.println(page);
        	// Hien thi text tra ve        	    		
        	return page;        	
        } catch (IOException e) {
        	e.printStackTrace();
        } catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}        			
	    return ""; 
	}
}
